package com.cskaoyan.service;

import com.cskaoyan.bo.PermissionBo;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.vo.permission.PermissionChildrenVo;
import com.cskaoyan.vo.permission.PermissionChildrenXVo;
import com.cskaoyan.vo.permission.PermissionVo;
import com.cskaoyan.vo.permission.SystemPermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public PermissionVo getPermission(Integer roleId) {

        PermissionVo permissionVo = new PermissionVo();

        //先查询当前角色的权限
        //先查询角色、权限关系表
        List<Integer> permissionIdList = permissionMapper.selectRolePermission(roleId);

        List<String> assignedPermissions = new ArrayList<>();
        for (Integer permissionId : permissionIdList) {

            //拿着id查权限名
            String api = permissionMapper.selectPermissionAPIById(permissionId);

            //处理权限
            int indexOf = api.indexOf("/");
            api = api.substring(indexOf + 1);
            api = api.replace("/", ":");

            assignedPermissions.add(api);
        }
        //权限加入
        permissionVo.setAssignedPermissions(assignedPermissions);

        //处理总表
        List<SystemPermissionVo> oneList = permissionMapper.selectOne();

        for (SystemPermissionVo systemPermissionVo : oneList) {

            Integer realId = systemPermissionVo.getRealId();

            //处理第二层
            List<PermissionChildrenVo> twoList = permissionMapper.selectTwo(realId);

            for (PermissionChildrenVo permissionChildrenVo : twoList) {

                //处理第三层
                Integer realIdChild = permissionChildrenVo.getRealId();
                List<PermissionChildrenXVo> threeList = permissionMapper.selectThree(realIdChild);

                for (PermissionChildrenXVo permissionChildrenXVo : threeList) {

                    String api = permissionChildrenXVo.getApi();
                    int indexOf = api.indexOf("/");
                    api = api.substring(indexOf + 1);
                    api = api.replace("/", ":");
                    permissionChildrenXVo.setId(api);
                }
                permissionChildrenVo.setChildren(threeList);
            }
            systemPermissionVo.setChildren(twoList);
        }
        permissionVo.setSystemPermissions(oneList);
        return permissionVo;
    }

    @Override
    public int updatePermission(PermissionBo permissionBo) {

        List<String> permissions = permissionBo.getPermissions();
        Integer roleId = permissionBo.getRoleId();

        //先删除该角色下的所有授权
        permissionMapper.deletePermission(roleId);

        //拿到所有权限
        List<PermissionChildrenXVo> list = permissionMapper.selectAllThree();

        for (PermissionChildrenXVo permissionChildrenXVo : list) {
            String api = permissionChildrenXVo.getApi();
            int indexOf = api.indexOf("/");
            api = api.substring(indexOf + 1);
            api = api.replace("/", ":");

            for (String s : permissions) {
                if (s.equals(api)) {
                    permissionMapper.addPermission(permissionChildrenXVo.getRealId(),roleId);
                }
            }
        }
        return 200;
    }
}
